package com.hfjava.practice04;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

import java.io.FileOutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

/**
 * @author Administrator
 * @version V1.0
 * @ProjectName:IntelliJ IDEA
 * @PackageName: com.hfjava.practice04
 * @ClassName: ProductParser
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @date 2020/1/6 19:48
 * @Copyright 大牛版板所有
 */
public class ProductParser {
    private static List<Product> productList = new ArrayList<>();
    private static Document doc = null;
    private static final String RELATIVE_PATH = "chapter06/data/Product.xml";

    static {
        SAXReader sr = new SAXReader();
        try {
            doc = sr.read(RELATIVE_PATH);
        } catch (DocumentException e) {
            e.printStackTrace();
        }

    }

    public static void showInfo()throws Exception{
        Element proElement = doc.getRootElement();
        Iterator iter = proElement.elementIterator();
        while (iter.hasNext()){
            Element element = (Element)iter.next();
            Product product = new Product();
            product.setProductNo(element.attributeValue("productNo"));
            product.setName(element.elementTextTrim("name"));
            product.setDiscount(element.elementTextTrim("discount"));
            product.setManufacturer(element.elementTextTrim("manufacturer"));
            product.setPrice(element.elementTextTrim("price"));
            product.setUnit(element.elementTextTrim("unit"));
            product.setStockCount(element.elementTextTrim("stockCount"));
            productList.add(product);
        }
        for (Product p:productList) {
            System.out.println("编号："+p.getProductNo()+"\n\t名称："+p.getName()+"\n\t折扣："+p.getDiscount()+
                    "\n\t生成厂家："+p.getManufacturer()+"\n\t单价："+p.getPrice()+"\n\t计价单位："+p.getUnit()+
                    "\n\t库存量："+p.getStockCount());
        }
        //productList.forEach(System.out::println);
    }

    // 查找
    public static void lookup(String productNo){
        boolean flag = false;
        for (Product p:productList) {
            if (p.getProductNo().equals(productNo)){
                System.out.println("编号："+p.getProductNo()+"\n\t名称："+p.getName()+"\n\t折扣："+p.getDiscount()+
                        "\n\t生成厂家："+p.getManufacturer()+"\n\t单价："+p.getPrice()+"\n\t计价单位："+p.getUnit()+
                        "\n\t库存量："+p.getStockCount());
                flag  =true;
                break;
            }
        }
        if (!flag){
            System.out.println("你查找的商品不存在！");
        }
    }

    // 保存
    public static void saveXml() throws Exception {
        OutputFormat format = new OutputFormat();
        XMLWriter xmlWriter = new XMLWriter(new FileOutputStream(RELATIVE_PATH),format);
        xmlWriter.write(doc);
        xmlWriter.close();
    }

    // 修改
    public static void modify(String productNo,String stockCount) throws Exception{
        SAXReader sr = new SAXReader();
        try {
            doc = sr.read(RELATIVE_PATH);
        } catch (DocumentException e) {
            e.printStackTrace();
        }
        Element proElement = doc.getRootElement();
        Iterator iter = proElement.elementIterator();
        boolean flag = true;
        while (iter.hasNext()){
            Element element = (Element)iter.next();
            if(element.attributeValue("productNo").equals(productNo)){
                element.element("stockCount").setText(stockCount);
                flag = false;
            }
        }
        if (flag){
            System.out.println("对不起，没有找到商品编号！");
        }
        saveXml();
    }

    // 购物商品
    public static void buy(String proNo,int num)throws Exception{
        double total = 0;
        boolean flag = false;
        String stockNum = null;
        for (Product p:productList) {
            if (p.getProductNo().equals(proNo)){
                if(num <= Double.valueOf(p.getStockCount())){
                    total = Double.valueOf(p.getPrice()) * Double.valueOf(p.getDiscount()) *num;
                    System.out.println("购买"+p.getName()+"商品成功，总金额为："+total+"元");
                    flag = true;
                    stockNum = String.valueOf((Integer.valueOf(p.getStockCount())-num));
                }
                else {
                    System.out.println(p.getName()+"库存量不足，购买失败！");
                }
            }
        }
        if (flag){
            modify(proNo,stockNum);
        }
    }


    public static void main(String[] args) throws Exception {
        showInfo();
        Scanner input = new Scanner(System.in);
        /*System.out.print("请输入查询商品的编号：");
        String productNo = input.next();
        //lookup(productNo);
        System.out.print("请输入要修改的库存数量：");
        String stockCount = input.next();
        modify(productNo,stockCount);*/

        System.out.print("请输入要购买商品的编号：");
        String proNo = input.next();
        System.out.print("请输入购买商品的数量：");
        int num = input.nextInt();
        buy(proNo,num);
        showInfo();

    }
}
